package com.spring.iosservice.web.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.spera.genutil.SpUtil;
import com.spera.genutil.TokenUtil;
import com.spring.iosservice.web.model.BuzProduct;
import com.spring.iosservice.web.model.EnregisterProduct;
import com.spring.iosservice.web.service.IPadApiService;
import com.spring.security.web.controller.BaseController;
import com.spring.security.web.dao.JedisCache;
import com.spring.security.web.model.BaseResult;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * IPAD签到签退接口
 * @author wojunyu
 *
 */
@Controller
@RequestMapping(value = "/ipadApi")
public class IPadApiController extends BaseController{
	
	@Resource private JedisCache jedisCache;
	@Resource private IPadApiService iPadApiService;
	
	@ResponseBody
    @RequestMapping(value="/buzProduct", method = RequestMethod.GET)
    @ApiOperation(value = "获取需要签到的业务类型及商品信息", httpMethod = "GET", response = Object.class, 
    	notes = "获取需要签到的业务类型及商品信息",produces = "application/json; charset=utf-8")
    public Object buzProduct(@ApiParam(required = true, name = "token", value = "token") /*@PathVariable*/@RequestParam String token,
    		HttpServletRequest req,HttpServletResponse resp) throws Exception{
		BaseResult ret = SpUtil.checkTokenNew(token, jedisCache);
		if("1".equals(ret.getStatus())){//验证通过
//			String user_id = jedisCache.hget(token,TokenUtil.USER_ID);
//			String username = jedisCache.hget(token,TokenUtil.USER_NAME);
			String kids_parkId = jedisCache.hget(token,TokenUtil.KIDS_PARK_ID);
			//根据门店查询需要签到签退的业务商品实体
			List<BuzProduct> dataSet = iPadApiService.buzProduct(kids_parkId);
			
/*						if(dataSet!=null&&dataSet.size()>0){
				for(BuzProduct bp:dataSet){
					if("1001".equals(bp.getBusinessTypesCode())||
							"1002".equals(bp.getBusinessTypesCode())){
						//如果是托班，早教，则需要跳转到课程界面选择课程
						if(bp.getEnregisterProductList()!=null&&bp.getEnregisterProductList().size()>0){
							for(EnregisterProduct one:bp.getEnregisterProductList()){
								one.setIsCourse("1");
							}
						}
					}
				}
			}*/
			ret.setDataset(dataSet);
		}
		return buildResultInfo(ret);
    }
	
	@ResponseBody
    @RequestMapping(value="/signOff", method = RequestMethod.POST)
    @ApiOperation(value = "IPAD文字签退", httpMethod = "POST", response = Object.class, notes = "IPAD签退",produces = "application/json; charset=utf-8")
    public Object signOff(@ApiParam(required = true, name = "token", value = "token") @RequestParam String token,
    		@ApiParam(required = true, name = "enregisterId", value = "登记id") @RequestParam String enregisterId,
    		@ApiParam(required = true, name = "fileid", value = "签字图片上传后返回的id") @RequestParam String fileid 
    		) throws Exception{
		BaseResult ret = SpUtil.checkTokenNew(token, jedisCache);
		if("1".equals(ret.getStatus())){//验证通过
			String user_id = jedisCache.hget(token,TokenUtil.USER_ID);
			String username = jedisCache.hget(token,TokenUtil.USER_NAME);
//			String kids_parkId = jedisCache.hget(token,TokenUtil.KIDS_PARK_ID);
			ret = iPadApiService.signOff(user_id,username,enregisterId,fileid);
		}
        return buildResultInfo(ret);
    }
	
	
	@ResponseBody
    @RequestMapping(value="/signIn", method = RequestMethod.POST)
    @ApiOperation(value = "教师确认进场", httpMethod = "POST", response = Object.class, notes = "教师确认进场",produces = "application/json; charset=utf-8")
    public Object signIn(@ApiParam(required = true, name = "token", value = "token") @RequestParam String token,
    		@ApiParam(required = true, name = "enregisterIds", value = "登记id") @RequestParam String enregisterIds
    		) throws Exception{
		BaseResult ret = SpUtil.checkTokenNew(token, jedisCache);
		if("1".equals(ret.getStatus())){//验证通过
			String user_id = jedisCache.hget(token,TokenUtil.USER_ID);
			String username = jedisCache.hget(token,TokenUtil.USER_NAME);
//			String kids_parkId = jedisCache.hget(token,TokenUtil.KIDS_PARK_ID);
			ret = iPadApiService.signIn(user_id,username,enregisterIds);
		}
        return buildResultInfo(ret);
    }
	
	@ResponseBody
    @RequestMapping(value="/signProvisions", method = RequestMethod.GET)
    @ApiOperation(value = "条款", httpMethod = "GET", response = Object.class, notes = "条款",produces = "application/json; charset=utf-8")
    public Object signProvisions(@ApiParam(required = true, name = "token", value = "token") @RequestParam String token
    		) throws Exception{
		BaseResult ret = SpUtil.checkTokenNew(token, jedisCache);
		if("1".equals(ret.getStatus())){//验证通过
			Map<String,Object> retM = Maps.newHashMap();
			retM.put("provisions", "签字条约~"/*JedisCont.getProParm()*/);
			ret.setDataset(retM);
		}
        return buildResultInfo(ret);
    }

}
